package com.example.mywechat;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class frdFragment extends Fragment {

    private static final String TAG = frdFragment.class.getSimpleName();
    private List<String> mList = new ArrayList<>();
    private List<StickyData> mDataList = new ArrayList<>();
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab02,container,false);
        initList();
        initData();
        initView();
        return view;

    }
    private void initList() {
        mList.add("男歌手|周杰伦");
        mList.add("男歌手|薛之谦");
        mList.add("男歌手|林俊杰");
        mList.add("男歌手|陈奕迅");
        mList.add("男歌手|许嵩");
        mList.add("男歌手|汪苏泷");
        mList.add("男歌手|毛不易");
        mList.add("男歌手|周深");
        mList.add("女歌手|邓紫棋");
        mList.add("女歌手|张靓颖");
        mList.add("女歌手|王菲");
        mList.add("女歌手|蔡依林");
        mList.add("女歌手|韩红");
        mList.add("女歌手|那英");
        mList.add("女歌手|黄龄");
        mList.add("组合|东方神起");
        mList.add("组合|TFBOYS");
        mList.add("组合|凤凰传奇");
        mList.add("组合|少女时代");
        mList.add("组合|逃跑计划");
        mList.add("组合|BIGBANG");
        mList.add("组合|EXO");
        mList.add("组合|BLACKPINK");
    }

    private void initData() {
        for (int i = 0; i < mList.size(); i++) {
            StickyData bean = new StickyData();

            String s = mList.get(i);
            String group = s.substring(0, s.indexOf("|"));
            String singer = s.substring(s.indexOf("|") + 1, s.length());

            bean.setGroup(group);
            bean.setSinger(singer);

            mDataList.add(bean);
        }
        Log.d(TAG, "initData: " + mDataList.size());
    }

    private void initView() {
        adapter adapter = new adapter(getActivity());

        RecyclerView rcvSticky = (RecyclerView) view.findViewById(R.id.rcv_sticky);
        final TextView tvArea = (TextView) view.findViewById(R.id.tv_sticky_header_view);

        rcvSticky.setLayoutManager(new LinearLayoutManager(getActivity()));
        rcvSticky.setHasFixedSize(true);
        rcvSticky.setAdapter(adapter);

        adapter.setStickyDataList(mDataList);

        rcvSticky.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                View stickyInfoView = recyclerView.findChildViewUnder(
                        tvArea.getMeasuredWidth() / 2, 5);

                if (stickyInfoView != null && stickyInfoView.getContentDescription() != null) {
                    tvArea.setText(String.valueOf(stickyInfoView.getContentDescription()));
                }

                View transInfoView = recyclerView.findChildViewUnder(
                        tvArea.getMeasuredWidth() / 2, tvArea.getMeasuredHeight() + 1);

                if (transInfoView != null && transInfoView.getTag() != null) {

                    int transViewStatus = (int) transInfoView.getTag();
                    int dealtY = transInfoView.getTop() - tvArea.getMeasuredHeight();

                    if (transViewStatus == adapter.HAS_STICKY_VIEW) {
                        if (transInfoView.getTop() > 0) {
                            tvArea.setTranslationY(dealtY);
                        } else {
                            tvArea.setTranslationY(0);
                        }
                    } else if (transViewStatus == adapter.NONE_STICKY_VIEW) {
                        tvArea.setTranslationY(0);
                    }
                }
            }
        });
    }
}